<template>
	<view>
		<view class="up-load" v-if="percent>0">
			<view style="margin-bottom: 35rpx;">
				正在下载安装包，请勿关闭软件......
			</view>
			<u-line-progress active-color="#2979ff" height="40" :show-percent="false" striped striped-active
				:percent="percent"></u-line-progress>
		</view>
		<u-modal v-model="show" :show-cancel-button="true" confirm-text="升级" cancel-text="退出" title="提示" @cancel="cancel"
			@confirm="confirm" content="请升级到新版本" />
	</view>
</template>

<script>
	var that
	export default {
		data() {
			return {
				show: false,
				percent: 0,
				downUrl: null
			}
		},
		onReady() {
			// this.show = true;
		},
		onLoad() {
			that = this
		},
		onShow() {
			// 在app中才更新
			// #ifdef APP-PLUS
			this.getAppInfo();
			// #endif

			// #ifdef H5
			this.getBuildApp();
			// #endif
		},
		methods: {
			// 查询是否需要更新
			getAppInfo() {
				// 不是生产环境 不更新
				if (global.reqHeard !== 'http://61.132.67.157:8287') {
					this.getBuildApp()
				} else {
					this.$u.post('/auth/appInfo', {}).then((res) => {
						if (res.code != 0) {
							this.$refs.uToast.show({
								title: '检测',
								message: res.message,
								type: 'error',
							});
							return false;
						}
						that.downUrl = `${global.reqHeard}/file/${res.data.appPath}`;
						var appversion = plus.runtime.version;
						console.log('版本号:', appversion);
						console.log('版本号:', res.data.appVersion);
						if (appversion != res.data.appVersion) {
							that.show = true
						} else {
							this.getBuildApp()
						}
					});
				}

			},
			// 获取首页菜单
			getBuildApp() {
				// console.log('getBuildApp');
				var token = uni.getStorageSync('token');
				uni.request({
					url: global.reqHeard + '/api/menus/buildApp',
					header: {
						Authorization: token,
						LoginEntryType: 'mobile',
					},
					method: 'GET',
					success: (res) => {
						// console.log(res);
						if (res.refreshedtoken) {
							uni.setStorage({
								key: 'token',
								data: res.refreshedtoken,
							});
						}
						if (res.data.status == 401) {
							uni.reLaunch({
								url: '/pages/login/index',
							});
						} else {
							var list = res.data[0].children;
							var farEndList = [];
							for (var item of list) {
								farEndList.push(item.path);
							}
							uni.setStorage({
								key: 'farEndList',
								data: farEndList,
							});
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
					},
					fail: (err) => {
						uni.reLaunch({
							url: '/pages/login/index',
						});
					},
				});
			},
			cancel() {
				plus.runtime.quit()
			},
			confirm() {
				var that = this;
				var downUrl = that.downUrl
				const downloadTask = uni.downloadFile({
					url: downUrl, //下载地址
					success: (resFile) => {
						plus.runtime.install(
							//安装
							resFile.tempFilePath, {
								force: true,
							},
							function(res) {
								plus.runtime.restart();
							}
						);
					},
				});
				downloadTask.onProgressUpdate((res) => {
					this.percent = res.progress
				});
			},
			closeModal() {
				uni.navigateBack();
			}
		}
	}
</script>

<style scoped lang="scss">
	.up-load {
		width: 700rpx;
		margin: 50rpx auto;
		padding-top: 150rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.u-full-content {
		background-color: #00C777;
	}

	.u-update-content {
		font-size: 26rpx;
		color: $u-content-color;
		line-height: 1.7;
		padding: 30rpx;
	}
</style>